//
//  AddInventoryViewController.m
//  WestAir
//
//  Created by s on 16/9/27.
//  Copyright © 2016年 Henry. All rights reserved.
//

#import "AddInventoryViewController.h"

#define cellHeight  50

@interface AddInventoryViewController ()

@end

@implementation AddInventoryViewController
@synthesize arrData,arrMainData,mainTableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self  initDataAndUI];
    
    
}


-(void)initDataAndUI{
    
     VIEW_CORNER(self.mainTableView, 4.0);
    
    
    NSString  *priceListNo=[HangBanInfo currentHangBan].priceListNo;
    NSDictionary  *dicJson=  [CMCCIOTIGUnity   getRequestJson:@{@"priceListNo":priceListNo}];
    
        [[NetworkFactory  alloc]  startPostRequestWithURL:GETPRODUCTLIST andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
            
            if ([responseJson[@"IsSuccess"]  integerValue]==1) {
                if ([responseJson[@"data"]  isKindOfClass:[NSArray  class]]) {
                    arrMainData=responseJson[@"data"];
                }
                else{
                    arrMainData=[@[]  mutableCopy];
                }
                
                [mainTableView reloadData];
                
                [self  resetFrame:arrMainData.count];
            }
            else{
               
                [self  resetFrame:0];
                
            }
            
            
            
        } andFailBlock:^(NSError *error) {
            [self  resetFrame:0];
            
        } ];

    }



#pragma mark Tableview Deleget

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrMainData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIdentifer=@"InventoryListCell";
    
    UITableViewCell   *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if(!cell){
                             
        cell=[[UITableViewCell  alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        
    }
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    NSDictionary  *dicData=arrMainData[indexPath.row];
    cell.textLabel.text=dicData[@"FName"];

    UIImageView  *imageLine=[[UIImageView alloc]  initWithFrame:CGRectMake(0, cellHeight-1, tableView.widthV, 1)];
    imageLine.backgroundColor=[UIColor  lightGrayColor];
    imageLine.alpha=0.2;
    [cell.contentView  addSubview:imageLine];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight ;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary  *dicData=arrMainData[indexPath.row];
    self.vertoryBlock(dicData);
    [self.navigationController  popViewControllerAnimated:YES];
    
}

- (IBAction)popBack:(id)sender {
    
    [self.navigationController  popViewControllerAnimated:YES];
    
}


-(void)resetFrame:(NSInteger )index{
    
    self.tableViewHeight.constant=index*cellHeight;
    
    if ( self.tableViewHeight.constant>SCREEN_HEIGHT-84) {
        
        self.tableViewHeight.constant=SCREEN_HEIGHT-84;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
